<?php
	/**
	 * 操作用户表的model
	 */
	class UserModel extends Model{
		
		public function __construct($table){
			parent::__construct($table);
		}
		
		
		/**
		 * 添加用户
		 */
		public function register($var=null){
			if(is_null($var)){
				return $this->add($_POST);
			}else{
				return $this->add($var);
			}
		}
		
		/**
		 * 检测登陆用户
		 */
		 public function checklogin($post){
		 	$row = $this->where($post)->find();
			return $row;
		 }
		
		/**
		 * 检测用户名是否存在
		 */
		 public function checkuser($username=null){
		 	
		 	$row = $this->where(array('username'=>$username))->find();
			return $row;
		 }
		 
		 /**
		  * 查询当前登陆用户的信息
		  */
		  public function getUserInfo($uid=null){

			  	$userinfo = M('user');	
			  	if(is_null($uid)){
			  		$username = session('username');
					$user = $userinfo->query('SELECT uid,username,ask,answer,accept,point,exp,face FROM hd_user WHERE username="'.$username.'" LIMIT 1');
				}else{
					$user = $userinfo->query('SELECT uid,username,ask,answer,accept,point,exp,face FROM hd_user WHERE uid="'.$uid.'" LIMIT 1');
				}
				//采纳率
				if($user[0]['answer']==0){
					$cnl = 0;
				}else{
					$cnl = round($user[0]['accept']/$user[0]['answer']*100);
				}
					$user[0]['cnl'] = $cnl.'%';
					return $user;
		  		}
	}
